package net.oschina.app.previousTest;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.robotium.solo.Solo;

import net.oschina.app.AppStart;
import net.oschina.app.R;

/**
 * Created by Administrator on 2017/7/1.
 */

public class ListViewTest2 extends ActivityInstrumentationTestCase2 {

    private Solo solo;

    public ListViewTest2() {
        super(AppStart.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testName() throws Exception {
/*        solo.sleep(5000);
        //liseview 变成view  要做向下类型的转换
        //用ID定位 到 列表
        ListView list = (ListView) solo.getView("net.oschina.app:id/listView");
        String title = "码云推荐 | 类型化 GET 参数的 Koa 中间件 koa-query-pretty";
        int index = -1;
        //getChildCount() 遍历这个列表
        for (int i = 0; i < list.getChildCount(); i++) {
            //list.getChildAt(i)  获取某一行
            //每一次取出列表的一行
            View view = list.getChildAt(i);
            //通过view这一行 往下找  找到 新闻标题的控件（用ID定位）
            TextView titleTV = (TextView) list.getChildAt(i).findViewById(R.id.tv_title);
            if (titleTV != null) {
                if (titleTV.getText().toString().contains(title)) {
                    index = i;
                    break;
                }
            }
        }
        Log.i("AUTO", "the index of new expected news titles :" + index);*/


        solo.sleep(5000);
        ListView view = (ListView) solo.getView("net.oschina.app:id/listView");
        String title = "源资讯 | 新型勒索病毒席卷欧洲；Win 10 源代码网上泄漏";
        int index = -1;
        for (int i = 0; i < view.getChildCount(); i++) {
             View view1 = view.getChildAt(i);
             TextView viewTV = (TextView) view1.findViewById(R.id.tv_title);
            if (viewTV != null){
                if (viewTV.getText().toString().contains(title)) {
                    index = i;
                    break;
                }
            }
        }
        Log.i("AUTO","the number is "+index);

        //第一个参数2 是指：制定点击列表的第几行，index从0开始
        //第二个参数0是指：点击当前页面第几个列表，index从0开始
        //第三个参数id 是指：点击列表里面的某个子控件；

        //点击当前页面 第一个列表的 第三行的id为tv_title的控件
        solo.clickInList(2,0,R.id.tv_title);



        //定位  发现按钮
        View parent = solo.getView("net.oschina.app:id/nav_item_me");
        View child = parent.findViewById(R.id.nav_tv_title);
        solo.clickOnView(child);

        //定位  发现按钮
        View views= solo.getView("net.oschina.app:id/nav_tv_title",2);
        solo.clickOnView(views);
        solo.sleep(3000);
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
        super.tearDown();
    }
}
